<template>
  <el-form :inline="true">
    <el-form-item>
      <el-select v-model="queryParam.saleUser" placeholder="选择销售人"
                 filterable clearable
                 @change="handleQuery">
        <el-option v-for="item in userList"
                   :key="item.id"
                   :label="item.nickName"
                   :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-select v-model="queryParam.status" clearable
                 placeholder="支付状态" @change="handleQuery">
        <el-option v-for="item in Dic.OrderStatus()"
                   :key="item.value"
                   :label="item.text"
                   :value="item.value"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-date-picker v-model="queryParam.dateArray"
                      :shortcuts="Dic.DateShortcuts"
                      @change="handleQuery"
                      type="daterange"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="YYYY-MM-DD"/>
    </el-form-item>
    <el-form-item>
      <el-input v-model="queryParam.keyword"
                @keyup.enter="handleQuery"
                placeholder="输入关键字"
                type="text"
                auto-complete="off"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
      <el-button type="warning" @click="listExport" :loading="loadingExport">导出</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData.list" v-loading="loading" border height="calc(100vh - 210px)">
    <el-table-column label="订单号" prop="orderSn" width="200" align="center"/>
    <el-table-column label="下单时间" prop="createTime" width="180" align="center" :formatter="Dic.Date"/>
    <el-table-column label="订单金额" prop="priceTotal" width="120" align="center"/>
    <el-table-column label="结算金额" prop="priceReal" width="120" align="center">
      <template v-slot="{row:{priceReal,orderStatus}}">
        <span :class="orderStatus === 1 ? 'success':'danger'" v-text="priceReal"/>
      </template>
    </el-table-column>
    <el-table-column label="其他费用" prop="otherAmount" width="100" align="center"/>
    <el-table-column label="订单详情" prop="goodsName" min-width="400" align="left">
      <template v-slot="{row:{goodsName}}">
        <div class="more-text" v-text="goodsName"/>
      </template>
    </el-table-column>
    <el-table-column label="用户信息" prop="memberPhone" width="150" align="center"/>
    <el-table-column label="支付状态" prop="orderStatus" width="100" align="center">
      <template v-slot="{row:{orderStatus}}">
        <el-tag :type="orderStatus === 1 ? 'success':'danger'" v-text="Dic.OrderStatus(orderStatus)"/>
      </template>
    </el-table-column>
    <el-table-column label="销售人" prop="saleName" width="100" align="center"/>
    <el-table-column label="协销人" prop="operatorName" width="100" align="center"/>
    <el-table-column label="操作" width="80" align="center" fixed="right">
      <template v-slot="{row}">
        <el-button link type="primary" @click="openRow(row)">
          查看
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination layout="->,total,sizes,prev,pager,next,jumper"
                 v-model:current-page="queryParam.page"
                 v-model:page-size="queryParam.limit"
                 :total="tableData.total" background
                 @current-change="loadList"
                 @size-change="loadList"/>
  <order-detail v-model="openRowDialog"
                :info="currentRow"
                :refresh="loadList"
                :close="()=>{this.openRowDialog = false}"/>
</template>

<script lang="ts">
import orderApi from "@/api/order";
import userApi from "@/api/user";
import Dic from "@/utils/dic";
import saveAs from "file-saver"
import {ElMessage} from "element-plus";
import orderDetail from "@/components/order/orderDetail.vue";

export default {
  name: "orderList",
  components: {orderDetail},
  data() {
    return {
      Dic,
      queryParam: {
        page: 1,
        limit: 20,
        keyword: "",
        saleUser: "",
        status: "",
        dateArray: []
      },
      tableData: {
        list: [],
        total: 0,
      },
      loading: false,
      loadingExport: false,
      userList: [],
      openRowDialog: false,
      currentRow: {},
    };
  },
  created() {
    this.loadSeller()
    this.loadList();
  },
  methods: {
    handleQuery() {
      this.queryParam.page = 1;
      this.loadList();
    },
    loadList() {
      this.loading = true;
      orderApi.list(this.queryParam).then(res => {
        console.log(res.data.list)
        this.loading = false;
        this.tableData = res.data
      })
    },
    listExport() {
      let [start, end] = this.queryParam.dateArray;
      if (!start || !end) {
        return ElMessage.info("请先选择时间")
      }
      this.loadingExport = true;
      orderApi.listExport({...this.queryParam, more: true}).then(res => {
        this.loadingExport = false;
        if (res.code === 500) {
          return ElMessage.warning("导出失败，请重试")
        }
        const dataBlob = new Blob([res]);
        saveAs(dataBlob, `${start}-${end}订单导出.xlsx`);
      })
    },
    loadSeller() {
      userApi.sellerList().then(res => this.userList = res.data)
    },
    openRow(row) {
      this.openRowDialog = true;
      this.currentRow = row
    }
  }
}
</script>
<style lang="scss" scoped>

</style>